<template>
  <div>
    <h3>新用户注册</h3>

    <div>
      <label>
        用户名: <input type="text" v-model="user.username"/>  <br>
      </label>
      <label>
        密码: <input type="text" v-model="user.password"/>  <br>
      </label>
      <label>
        年龄: <input type="text" v-model="user.age"/>  <br>
      </label>
      <label>
        其他: <input type="text" v-model="user.extension"/>  <br>
      </label>
      <label>
        性别: <input type="radio" v-model="user.sex" value="0"/> 女 &nbsp;&nbsp;
              <input type="radio" v-model="user.sex" value="1"/> 男  <br>
      </label>

      <button type="button" @click="addUser">确认</button>
      <br>
      <router-link to="/login">立即登录</router-link>
    </div>
  </div>
</template>

<script>
  import { Message } from "element-ui";

  export default {
    name: 'AddUserTest',
    data() {
      return {
        user: {
          username: "",
          password: "",
          age: null,
          extension: "",
          sex: null
        }
      }
    },
    methods: {
      addUser() {
        let _this = this;
        let url = "user/register";
        if (this.validate()) {
          this.postRequest(url, this.user).then(res => {
            console.log(res);
            if (res.code === 0) {
              Message.success({message: res.msg});
              // 跳转到登录页
              setTimeout(() => {
                _this.$router.replace("/login");
              }, 2000);
            }

          }).catch();
        }
      },
      validate() {
        return this.user.username !== ""
          && this.user.password !== ""
          && this.user.extension !== ""
          && this.user.age !== null
          && this.user.sex !== null
      }
    },

  }
</script>

<style scoped>

</style>
